package com.ruoyi.exam.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.exam.domain.TExamPaper;
import com.ruoyi.exam.domain.vo.ExamPaperVo;
import com.ruoyi.exam.mapper.TExamPaperMapper;
import com.ruoyi.exam.mapper.TQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.exam.mapper.TExamPaperAnswerMapper;
import com.ruoyi.exam.domain.TExamPaperAnswer;
import com.ruoyi.exam.service.ITExamPaperAnswerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生试卷答案Service业务层处理
 *
 * @author Bandit
 * @date 2022-11-01
 */
@Service
public class TExamPaperAnswerServiceImpl implements ITExamPaperAnswerService
{
    @Autowired
    private TExamPaperAnswerMapper tExamPaperAnswerMapper;

    @Autowired
    private TExamPaperMapper tExamPaperMapper;

    @Autowired
    private TQuestionMapper tQuestionMapper;

    /**
     * 查询学生试卷答案
     *
     * @param id 学生试卷答案主键
     * @return 学生试卷答案
     */
    @Override
    public TExamPaperAnswer selectTExamPaperAnswerById(Long id)
    {
        return tExamPaperAnswerMapper.selectTExamPaperAnswerById(id);
    }

    /**
     * 查询学生试卷答案列表
     *
     * @param tExamPaperAnswer 学生试卷答案
     * @return 学生试卷答案
     */
    @Override
    public List<TExamPaperAnswer> selectTExamPaperAnswerList(TExamPaperAnswer tExamPaperAnswer)
    {
        return tExamPaperAnswerMapper.selectTExamPaperAnswerList(tExamPaperAnswer);
    }

    @Override
    public List<TExamPaperAnswer> selectTExamPaperAnswerByUserId(Long userId) {
        return tExamPaperAnswerMapper.selectTExamPaperAnswerByUserId(userId);
    }

    @Override
    public List<SysUser> unDispatchStuList(Long examPaperId) {
        return tExamPaperAnswerMapper.unDispatchStuList(examPaperId);
    }

    /**
     * 新增学生试卷答案
     *
     * @param tExamPaperAnswer 学生试卷答案
     * @return 结果
     */
    @Override
    public int insertTExamPaperAnswer(TExamPaperAnswer tExamPaperAnswer)
    {
        tExamPaperAnswer.setCreateTime(DateUtils.getNowDate());
        return tExamPaperAnswerMapper.insertTExamPaperAnswer(tExamPaperAnswer);
    }

    @Override
    @Transactional
    public int insertTExamPaperAnswerBatch(List<TExamPaperAnswer> tExamPaperAnswerList) {
        ExamPaperVo examPaperVo = null;
        int score = 0;
        for (TExamPaperAnswer examPaperAnswer : tExamPaperAnswerList) {
            if (examPaperVo == null) {
                Long examPaperId = examPaperAnswer.getExamPaperId();
                examPaperVo = tExamPaperMapper.selectExamPaperVoById(examPaperId);
                List<Long> questionArrays = JSONArray.parseArray(examPaperVo.getContent()).toList(Long.class);
                score = tQuestionMapper.getScoreByList(questionArrays);
            }
            examPaperAnswer.setPaperScore((long) score);
            examPaperAnswer.setQuestionCorrect(0L);
            examPaperAnswer.setCreateTime(new Date());
            tExamPaperAnswerMapper.insertTExamPaperAnswer(examPaperAnswer);
        }
        return tExamPaperAnswerList.size();
    }

    /**
     * 修改学生试卷答案
     *
     * @param tExamPaperAnswer 学生试卷答案
     * @return 结果
     */
    @Override
    public int updateTExamPaperAnswer(TExamPaperAnswer tExamPaperAnswer)
    {
        return tExamPaperAnswerMapper.updateTExamPaperAnswer(tExamPaperAnswer);
    }

    /**
     * 批量删除学生试卷答案
     *
     * @param ids 需要删除的学生试卷答案主键
     * @return 结果
     */
    @Override
    public int deleteTExamPaperAnswerByIds(Long[] ids)
    {
        return tExamPaperAnswerMapper.deleteTExamPaperAnswerByIds(ids);
    }

    /**
     * 删除学生试卷答案信息
     *
     * @param id 学生试卷答案主键
     * @return 结果
     */
    @Override
    public int deleteTExamPaperAnswerById(Long id)
    {
        return tExamPaperAnswerMapper.deleteTExamPaperAnswerById(id);
    }
}
